package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.DateUtils;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.mapper.WorkerBankMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IWorkBankService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerBankServiceImpl extends ServiceImpl<WorkerBankMapper, BankAccount> implements IWorkBankService {
    @Resource
    private WorkerBankMapper workerBankMapper;

    /**
     * 新增或更新银行账号
     *
     * @param bankaccounDTO
     */
    @Override
    public void addOrUpdate(BankAccountUpsertReqDTO bankaccounDTO) {

        // 获取当前用户ID
        Long id = UserContext.currentUserId();

        // 查询银行账户
        List<BankAccount> bankAccounts = baseMapper.selectList(new QueryWrapper<BankAccount>().eq("id", id));

        if (bankAccounts.size() > 1) {
            throw new RuntimeException("数据库中存在多个与ID匹配的记录，ID: " + id);
        }

        BankAccount bankAccount = bankAccounts.isEmpty() ? null : bankAccounts.get(0);

        if (ObjectUtil.isNotEmpty(bankAccount)) {
            // 更新操作
            BankAccount bank = BeanUtils.toBean(bankaccounDTO, BankAccount.class);
            bank.setId(id);
            bank.setUpdateTime(LocalDateTime.now());
            baseMapper.updateById(bank);
        } else {
            // 新增操作
            BankAccount bank = BeanUtils.toBean(bankaccounDTO, BankAccount.class);
            bank.setId(id);
            bank.setCreateTime(LocalDateTime.now());
            bank.setUpdateTime(LocalDateTime.now());
            baseMapper.insert(bank);
        }


    }

    /**
     * 查询当前用户的银行账号
     */
    @Override
    public BankAccountResDTO currentUserBankAccount() {
        // 获取用户id
        Long id = UserContext.currentUserId();

        // 根据id查询
        BankAccount bankAccount = workerBankMapper.selectById(id);

        // 如果查询不到记录，返回一个空的BankAccountResDTO对象
        if (bankAccount == null) {
            return new BankAccountResDTO();
        }
//        BankAccountResDTO resDTO = new BankAccountResDTO();
        // 返回数据
        BankAccountResDTO dto = BeanUtil.toBean(bankAccount, BankAccountResDTO.class);
        return dto;
    }
}

